package com.rookie.tool.module.util.ai.util;

import com.rookie.tool.module.util.ai.constant.CommonConstant;
import com.rookie.tool.module.util.ai.entity.Car;
import com.rookie.tool.module.util.ai.entity.Dish;
import com.rookie.tool.module.util.ai.entity.Goods;
import com.rookie.tool.module.util.ai.entity.Landmark;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author rookie
 */
@Slf4j
public class GoodsUtils {

    private final static String TOKEN = TokenUtils.getToken(CommonConstant.BAI_DU_CLIENT_ID, CommonConstant.BAI_DU_CLIENT_ID);

    @Getter
    @AllArgsConstructor
    private enum ItemUrl {

        /**
         * 植物识别
         */
        PANT_URL("植物识别", "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"),
        /**
         * 菜品识别
         */
        DISH_URL("菜品识别", "https://aip.baidubce.com/rest/2.0/image-classify/v2/dish"),

        /**
         * 地标识别
         */
        LANDMARK_URL("地标识别", "https://aip.baidubce.com/rest/2.0/image-classify/v1/landmark"),

        /**
         * 果蔬识别
         */
        INGREDIENT_URL("果蔬识别", "https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient"),

        CAR_URL("车型识别", "https://aip.baidubce.com/rest/2.0/image-classify/v1/car"),
        /**
         * 动物识别
         */
        ANIMAL_URL("动物识别", "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal");

        private String msg;

        private String url;


    }

    /**
     * 植物识别
     */
    public static Goods extractPlant(String img) {
        String url = appendToken(ItemUrl.PANT_URL.getUrl());
        return TokenUtils.restTemplate.postForObject(url, AiUtils.appendParam(img,null), Goods.class);
    }

    /**
     * 菜品识别
     *
     * @param img       图片本地路径
     * @param threshold 识别率，默认是0.95
     * @return 结果
     */
    public static Dish extractDish(String img, Float threshold) {
        String param = "image=";
        if (threshold != null) {
            param = "filter_threshold=" + threshold + "&" + param;
        }
        String url = appendToken(ItemUrl.DISH_URL.getUrl());
        return TokenUtils.restTemplate.postForObject(url,  AiUtils.appendParam(img,param), Dish.class);
    }

    /**
     * 地标识别
     *
     * @param img img
     * @return 结果
     */
    public static Landmark extractLandmark(String img) {
        String url = appendToken(ItemUrl.LANDMARK_URL.getUrl());
        return TokenUtils.restTemplate.postForObject(url, AiUtils.appendParam(img,null), Landmark.class);
    }

    /**
     * 果蔬识别
     *
     * @param img img
     * @return 结果
     */
    public static Goods extractIngredient(String img) {
        String url = appendToken(ItemUrl.INGREDIENT_URL.getUrl());
        return TokenUtils.restTemplate.postForObject(url, AiUtils.appendParam(img,null), Goods.class);
    }

    /**
     * 动物识别
     *
     * @param img 图片
     * @return 结果
     */
    public static Goods extractAnimal(String img) {
        String url = appendToken(ItemUrl.ANIMAL_URL.getUrl());
        return TokenUtils.restTemplate.postForObject(url, AiUtils.appendParam(img,null), Goods.class);
    }

    public static Car extractCar(String img) {
        String url = appendToken(ItemUrl.CAR_URL.getUrl());
        return TokenUtils.restTemplate.postForObject(url, AiUtils.appendParam(img,null), Car.class);
    }



    /**
     * 填充token
     *
     * @param url 接口路径
     * @return 结果
     */
    private static String appendToken(String url) {
        return url + "?access_token=" + TOKEN;
    }

}
